﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;

namespace RobotControl
{
    public partial class Form_Main : Form
    {
        public byte ShenDuRxCount = 0;
        byte ShenDu1, ShenDu2;
        private void ShenDuCom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte rx;
            if (ComClosing[4])
                return;
            ComListening[4] = true;
            while (ShenDuCom.BytesToRead != 0)
            {
                int irx = ShenDuCom.ReadByte();
                if (irx == -1)
                    break;
                rx = (byte)irx;
                ShenDuRxCount++;
                switch (ShenDuRxCount)
                {
                    case 1:
                        if (rx != 0xaa)
                            ShenDuRxCount = 0;
                        break;
                    case 2:
                        ShenDu1 = rx;
                        break;
                    case 3:
                        ShenDu2 = rx;
                        break;
                    case 4:
                        if (rx == 0xed)
                        {
                            double shendu=BytesOP.GetHighNibble(ShenDu1)*1000+BytesOP.GetLowNibble(ShenDu1)*100
                                + BytesOP.GetHighNibble(ShenDu2) * 10 + BytesOP.GetLowNibble(ShenDu2) * 1;
                            shendu = shendu / 10;
                            ExternChangeParamList(ParamsListAddr.深度, shendu.ToString("F1"));
                            ExternAddText("收到深度计数据:" + shendu.ToString("F1") + "米");
                        }
                        ShenDuRxCount = 0;
                        break; 
                    default:
                        ShenDuRxCount = 0;
                        break;
                }                
            }
            ComListening[4] = false;
        }
    }
}
